﻿using HomagGroup.Base.Interfaces;
using HomagGroup.Base.UI;
using HomagChina.Nesting.Router;
using HomagChina.Nesting.Views;
using Proxy;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace HomagChina.Nesting
{
    public class MainWindowViewModel : HomagGroup.Base.UI.NotifyPropertyChangedBase
    {
        #region Conponents

        private ObservableCollection<FunctionKeyBarItemDataModel> _functionKeyBarMenu;

        public ObservableCollection<FunctionKeyBarItemDataModel> FunctionKeyBarMenu
        {
            get => _functionKeyBarMenu;
            set { _functionKeyBarMenu = value; OnPropertyChanged(); }
        }

        private UIElement _ContentElement;

        public UIElement ContentElement
        { get => _ContentElement; set { _ContentElement = value; OnPropertyChanged(); } }

        public RouterModel routerModel { get; set; }
        public ObservableCollection<RouterItemDataModel> RouterItems { get; }

        private RouterItemDataModel _CurrentRouter;

        public RouterItemDataModel CurrentRouter
        {
            get => _CurrentRouter;
            set
            {
                if (value == null) return;
                _CurrentRouter = value;
                OnPropertyChanged();
                while (RouterItems.LastOrDefault() != value)
                {
                    RouterItems.Remove(RouterItems.Last());
                }
                if (RouterItems.Count() > 2)
                {
                    RouterItems.RemoveAt(RouterItems.Count - 2);
                }

                NavigateToExecute(value.ID);
            }
        }

        #endregion Conponents

        public MainWindowViewModel(IErrorHandler errorHandler)
        {
            #region Conponents

            RouterItems = new ObservableCollection<RouterItemDataModel>();
            FunctionKeyBarMenu = new ObservableCollection<FunctionKeyBarItemDataModel>();
            UIAdjustments.CurrentThreadLanguageManager.CultureChanged += LanguageManagerCultureChanged;

            #endregion Conponents

            InitNavigate();
            SerilogHandle.Logger_System.Information("用户开启了程序");
        }

        public void InitNavigate()
        {
            try
            {
                routerModel = new RouterModel(this, true);
                var routerInfo = routerModel.RouterDictionary.ContainsKey("1") ? routerModel.RouterDictionary["1"] : null;
                if (!RouterItems.Any(item => item.ID == routerInfo.ID)) RouterItems.Add(routerInfo);
                ContentElement = routerInfo.userControl;
                FunctionKeyBarMenu = routerInfo.functionKeyBarItems;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_UI.Error(ex.Message);
            }
        }

        public void NavigateToExecute(object parameter = null)
        {
            try
            {
                var Id = parameter == null ? "1" : parameter as string;
                var routerInfo = routerModel.RouterDictionary.ContainsKey(Id) ? routerModel.RouterDictionary[Id] : null;
                if (!RouterItems.Any(item => item.ID == routerInfo.ID) || routerInfo.ID == "1")
                {
                    if (routerInfo.ID != "1") RouterItems.Add(routerInfo);

                    if (!routerInfo.keepalive)
                    {
                        routerInfo.userControl.DataContext = routerModel.UpdateBindDataContext(routerInfo).ViewModel;
                        routerInfo.ViewModel = routerModel.UpdateBindDataContext(routerInfo).ViewModel;
                    }

                    ContentElement = routerInfo.userControl;
                    FunctionKeyBarMenu = routerInfo.functionKeyBarItems;
                };
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_UI.Error(ex.Message);
            }
        }

        public void LanguageManagerCultureChanged(object sender, System.EventArgs e)
        {
            try
            {
                routerModel = new RouterModel(this, false);
                var UpdateRouters = new ObservableCollection<RouterItemDataModel>();
                foreach (var item in RouterItems)
                {
                    UpdateRouters.Add(routerModel.RouterDictionary[item.ID]);
                }
                RouterItems.Clear();
                RouterItems.AddRange(UpdateRouters);
                ContentElement = routerModel.RouterDictionary[RouterItems.Last().ID].userControl;
                FunctionKeyBarMenu = routerModel.RouterDictionary[RouterItems.Last().ID].functionKeyBarItems;
            }
            catch (Exception ex)
            {
                SerilogHandle.Logger_UI.Error(ex.Message);
            }
        }
    }
}